
<?php
/*
  `idOrden` INT NOT NULL,
  `preStep` VARCHAR(3) NOT NULL,
  `newStep` VARCHAR(3) NOT NULL,
  `modificationDate` DATETIME NOT NULL,
  `modificationUser` VARCHAR(20) NOT NULL,
  `comment` VARCHAR(150)
*/
  include_once 'OrdenDao.php';

    function insertarLog($idOrden,$preStep,$nextStep,$user,$comment){

      $array_prop = parse_ini_file('DataAcces.properties');
      $con = mysqli_connect($array_prop['servidor'], $array_prop['usuario'], $array_prop['contra'], $array_prop['base']);
      /* verificar la conexión */
      if (mysqli_connect_errno()) {
        printf("Conexión fallida: %s\n", mysqli_connect_error());
        exit();
      }

      if($idOrden==0){
        $id = getMaxOrden($user);
        print_r($id);
      }

      if(isset($comment) && strlen($comment)>0){

        $selectColor = "SELECT r.color from u225435723_orden.SEC_USER u INNER JOIN u225435723_orden.SEC_ROLE r ON (r.roleCode=u.roleCode) WHERE u.userName = '".$user."'";
        if ($resultado = mysqli_query($con, $selectColor)) {
          if($row = mysqli_fetch_assoc($resultado)){
            $insertSQL = "INSERT INTO u225435723_orden.STEP_LOG (idOrden,preStep, newStep, modificationDate,modificationUser,comment,color) VALUES ("
                      . $idOrden . ",'".$preStep."','".$nextStep."',now(),'".$user."','".$comment."','".$row[color]."'"
                      .")";
          }
        }
      } else {
        $insertSQL = "INSERT INTO u225435723_orden.STEP_LOG (idOrden,preStep, newStep, modificationDate,modificationUser) VALUES ("
                      . $idOrden . ",'".$preStep."','".$nextStep."',now(),'".$user."'"
                      .")";
      }
      mysqli_query($con,$insertSQL);
      mysqli_commit($con);
      mysqli_close($con);

    }

    function selectLogsByIdDesc($idOrden) {
      $array_prop = parse_ini_file('DataAcces.properties');
      $enlace = mysqli_connect($array_prop['servidor'], $array_prop['usuario'], $array_prop['contra'], $array_prop['base']);
      if (mysqli_connect_errno()) {
          printf("Conexión fallida: %s\n", mysqli_connect_error());
          exit();
      }
      $consulta = "SELECT l.idOrden, l.preStep, l.newStep, l.modificationDate, CONCAT_WS(' ',u.firstName,u.LastName) modificationUser, l.comment,l.color  "
                  ."FROM u225435723_orden.STEP_LOG l INNER JOIN u225435723_orden.SEC_USER u ON (u.userName=l.modificationUser) "
                  ."WHERE idOrden = ".$idOrden." AND CHAR_LENGTH(comment)>0 ORDER BY modificationDate DESC";
      if ($resultado = mysqli_query($enlace, $consulta)) {
          while ($row = mysqli_fetch_assoc($resultado)) { $results[] = $row; }
          mysqli_free_result($resultado);
      }
      mysqli_close($enlace);
      if (isset($results)) return $results;
    }

?>